package com.sxxm.qxgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxxm.qxgl.entity.SysRole;
import com.sxxm.qxgl.entity.SysUser;
import com.sxxm.qxgl.entity.UserMenu;
import com.sxxm.qxgl.mapper.SysRoleMapper;
import com.sxxm.qxgl.mapper.UserMenuMapper;
import com.sxxm.qxgl.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxxm.qxgl.utils.TreeUtils;
import com.sxxm.qxgl.vo.Result;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author qjw
 * @since 2021-09-14
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    SysRoleMapper sysRoleMapper;
    UserMenuMapper userMenuMapper;
    public SysRoleServiceImpl(SysRoleMapper sysRoleMapper,UserMenuMapper userMenuMapper){
        this.sysRoleMapper = sysRoleMapper;
        this.userMenuMapper = userMenuMapper;
    }


    @Override
    public Result cx(Long id ,String name, int pag, int sl) {
        Page<SysRole> page = new Page<>(pag, sl);
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        if (id != null) {
            queryWrapper.eq("id", id);
        }
        return Result.ok().message("成功").data(sysRoleMapper.selectMapsPage(page, queryWrapper));
    }

    @Override
    public Result ModifyThe(SysRole sysRole) {
        QueryWrapper<SysRole> Wrapper = new QueryWrapper<>();
        Wrapper.lambda().eq(SysRole::getId, sysRole.getId());
        //修改条件
        SysRole user = sysRoleMapper.selectOne(Wrapper);
        sysRole.setId(user.getId());
        int a = sysRoleMapper.updateById(sysRole);
        if (a <= 0) {
            return Result.error().message("修改失败");
        }
        return Result.ok().message("修改成功");
    }

    @Override
    public Result Addition(SysRole sysUser) {
        if (sysRoleMapper.insert(sysUser) <= 0) {
            return Result.error().message("添加失败");
        }
        return Result.ok().message("添加成功");

    }

    @Override
    public Result Delete(long id) {
        if (sysRoleMapper.deleteById(id) <= 0) {
            return Result.error().message("删除失败");
        }
        return Result.ok().message("删除成功");
    }

    @Override
    public Result zt(long id) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRole::getId, id);
        //修改条件
        SysRole user = sysRoleMapper.selectOne(queryWrapper);
        SysRole user1 = new SysRole();
        user1.setId(user.getId());
        if (sysRoleMapper.selectById(id).getStatus() == 0) {
            user1.setStatus(1);
        }else {
            user1.setStatus(0);
        }
        if (sysRoleMapper.updateById(user1) <= 0) {
            return Result.error().message("状态修改失败");
        }
        return Result.ok().message("状态修改成功");
    }

    @Override
    public Result xcyh(Integer id,Integer userType) {
        List<SysUser> result = null;
        if (userType == 0) {
            result= sysRoleMapper.AllExam(Math.toIntExact(id));
        }else {
            String userIds =  sysRoleMapper.selectUserRole(id);
            result = sysRoleMapper.selectUserRole1(userIds);
        }
        return Result.ok().message("查询成功").data(result);
    }

    @Override
    public Result getMenu(String userID) {
        QueryWrapper<UserMenu> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(UserMenu::getUserId, userID);
        List<UserMenu> result = userMenuMapper.selectList(sectionQueryWrapper);
        if (result.size() == 0) {
            return Result.error().message("没有拥有权限的菜单！");
        }
        List<UserMenu> userMenus =  TreeUtils.getUserMenuTree(result,new Long(0));
        return Result.ok().data(userMenus);
    }
//    @Override
//    public Result tjyh(List<Integer> id) {
//        sysRoleMapper.insertBatchSomeColumn();
//        return null;
//    }
}
